<?php

namespace Admin\Controller;

use Admin\Form\BorrowReturnForm;
use Admin\Controller\IndexController;
use Admin\Model\DBO\SearchBookDBO;
use Admin\Model\DBO\BorrowReturnDBO;
use Application\Model\DBO\ProcessReaderDBO;
use Zend\Form\Annotation\AnnotationBuilder;

class ReturnDocumentController extends IndexController {

    public $form;

    public function getForm() {
        if (!$this->form) {
            $user = new BorrowReturnForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }

        return $this->form;
    }

    public function indexAction() {
        // Nếu quyền bằng 2 thì ko duoc vao
        $this->checkRole2();
        // kiem tra neu chua dang nhap thi quay ve lai trang dang nhap
        // Neu dang nhap roi thi hien thi len sach sach nguoi dung
        // Lay thuc the dang nhap dua ra layout
        $this->layout()->identity = $this->getAuthService()->getIdentity();
        $form = $this->getForm();
        $request = $this->getRequest();
        $myMessage = array();
        if ($request->isPost()) {
            $inputData = $request->getPost();
            $form->setData($inputData);
            $profilePhoto = "";
            $paginator = null;
            $allAuthor = array();
            // Kiem tra du lieu vao co hop le hay ko
            if ($form->isValid()) {
                $data = $form->getData();
                $objectBorrowReturn = new BorrowReturnDBO($this->getServiceLocator());
                $cardBarcode = $data['cardBarcode'];
                $bookBarcode = $data['bookBarcode'];
                $button = $data['returnDocument'];
                // Neu nhấn nút mượn tài liệu và tồn tại mã thẻ và mã tài liệu thì thưc hiện mượn tài liệu
                if ($button != null && $cardBarcode != null && $bookBarcode != null) {

                    // Phai kt xem tai lieu do o trang thai muon hay co san
                    // kt tra xem ma ban doc nhap vao co dược mượn theo chính sách lưu thông hay không
                    // Neu dung 2 dk tren moi được mượn tài liệu
                    // Khi mượn tài liệu thì phải insert vào bảng lưu thông trong ngày
                    // --> cuối cùng thưc hiện mượn tài liệu từ đối tượng BorrowReturn
                    $result = $objectBorrowReturn->returnDocument($cardBarcode, $bookBarcode);
                    if ($result) {
                        $myMessage['returnOK'] = 'returnOK';
                    } else {
                        $myMessage['returnError'] = 'returnError';
                    }
                } else if ($button != null && ($cardBarcode == null || $bookBarcode == null)) {
                    $myMessage['notExistInfo'] = 'notExistInfo';
                }
                if ($cardBarcode != null) {
                    // Tim kiem thong tin ban doc va thong tin muon tra sach
                    $infoReader = $objectBorrowReturn->getInfoReader($cardBarcode);
                    // Neu ton tai thong tin ban doc thi tim tiep danh sach tai lieu dang muon
                    if (isset($infoReader) && $infoReader != null) {
                        $form->setData($infoReader);
                        $profilePhoto = $infoReader['profilePhoto'];
                        $resultSet = $objectBorrowReturn->searchInfoBookBorrow($cardBarcode);
                        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
                        // Tim kiếm tác gả dựa vào mã sách
                        foreach ($resultSet as $key) {
                            $bookBarcodeTmp = $key['bookBarcode'];
                            $stringAuthor = "";
                            $authors = $objectSearchBook->searchBookAuthor($bookBarcodeTmp);
                            foreach ($authors as $author) {
                                $stringAuthor .= $author['author'] . '-';
                            }
                            $allAuthor[$bookBarcodeTmp] = $stringAuthor;
                        }
                        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectBorrowReturn->searchInfoBookBorrow($cardBarcode));
                        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
                        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
                        $paginator->setItemCountPerPage(7);
                        $paginator->setPageRange(5);
                    } else {
                        $myMessage['notFoundReader'] = 'notFoundReader';
                    }
                }
                // Tim kiem thong tin sach; thong tin ban doc; Thong tin sach muon
                if ($bookBarcode != null && $cardBarcode == null) {
                    // Tim kiem thong tin sach;
                    $dataInfoBook = $objectBorrowReturn->getInfoBook($bookBarcode);
                    if ($dataInfoBook) {
                        $form->setData($dataInfoBook);
                    } else {
                        $myMessage['notFoundBook'] = 'notFoundBook';
                    }
                    // thong tin ban doc dua vao ma sach muon
                    $infoReader = $objectBorrowReturn->getInfoReaderFromBookBarcode($bookBarcode);
                    if (isset($infoReader) && $infoReader != null) {
                        $form->setData($infoReader);
                        $profilePhoto = $infoReader['profilePhoto'];
                        $resultSet = $objectBorrowReturn->searchInfoBookBorrow($infoReader['cardBarcode']);
                        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
                        // Tim kiếm tác gả dựa vào mã sách
                        foreach ($resultSet as $key) {
                            $bookBarcodeTmp = $key['bookBarcode'];
                            $stringAuthor = "";
                            $authors = $objectSearchBook->searchBookAuthor($bookBarcodeTmp);
                            foreach ($authors as $author) {
                                $stringAuthor .= $author['author'] . '-';
                            }
                            $allAuthor[$bookBarcodeTmp] = $stringAuthor;
                        }
                        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectBorrowReturn->searchInfoBookBorrow($infoReader['cardBarcode']));
                        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
                        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
                        $paginator->setItemCountPerPage(10);
                        $paginator->setPageRange(5);
                    }
                } else if ($bookBarcode != null) {
                    $dataInfoBook = $objectBorrowReturn->getInfoBook($bookBarcode);
                    if ($dataInfoBook) {
                        $form->setData($dataInfoBook);
                    } else {
                        $myMessage['notFoundBook'] = 'notFoundBook';
                    }
                }
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            // Tra ve form neu du lieu truyen len server theo phuong thuc Post()
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
                'profilePhoto' => $profilePhoto,
                'paginator' => $paginator,
                'allAuthor' => $allAuthor,
            );
        }
        // tra ve form neu chua pha la phuong thuc Post
        return array(
            'form' => $form,
        );
    }

    public function updateTimeBorrowAction() {
        $bookBarcode = $this->params()->fromRoute('id', 0);
        $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
        $form = $this->getForm();
        $myMessage = array();
        $profilePhoto = "";
        $paginator = null;
        $allAuthor = array();
        // Gia han tai lieu
        $result = $objectProcessReader->addTimeBorrow($bookBarcode);
        if (!$result) {
            $myMessage['addTimeError'] = 'addTimeError';
        }
        // Lay thong tin ban doc
        $objectBorrowReturn = new BorrowReturnDBO($this->getServiceLocator());
        $infoReader = $objectBorrowReturn->getInfoReaderFromBookBarcode($bookBarcode);
        if (isset($infoReader) && $infoReader != null) {
            $form->setData($infoReader);
            $profilePhoto = $infoReader['profilePhoto'];
            $resultSet = $objectBorrowReturn->searchInfoBookBorrow($infoReader['cardBarcode']);
            $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
            // Tim kiếm tác gả dựa vào mã sách
            foreach ($resultSet as $key) {
                $bookBarcodeTmp = $key['bookBarcode'];
                $stringAuthor = "";
                $authors = $objectSearchBook->searchBookAuthor($bookBarcodeTmp);
                foreach ($authors as $author) {
                    $stringAuthor .= $author['author'] . '-';
                }
                $allAuthor[$bookBarcodeTmp] = $stringAuthor;
            }
            $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectBorrowReturn->searchInfoBookBorrow($infoReader['cardBarcode']));
            $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(5);
        }
        return array(
            'form' => $form,
            'myMessage' => $myMessage,
            'profilePhoto' => $profilePhoto,
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
        );
    }

}